﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using jnxceq.Model;
using jnxceq.Common.Log;
//using Monitor.UI;
namespace jnxceq.Common
{
    class Tools
    {
        #region  绘图

        /// <summary>
        /// 画圆
        /// </summary>
        /// <param name="g"> 画布</param>
        /// <param name="centerPoint">圆心点</param>
        /// <param name="r">半径</param>
        /// <param name="fill">是否填充</param>
        /// <param name="fillColor">填充颜色</param>
        /// <param name="drawOutLine">是否要外框</param>
        /// <param name="outLineColor">外框颜色</param>
        /// <param name="outLineWidth">外框线条宽度</param>
        public static void DrawCircle(Graphics g, PointF centerPoint, float r, bool fill, Color fillColor, bool drawOutLine,
            Color outLineColor, float outLineWidth)
        {
            try
            {
                g.TranslateTransform(centerPoint.X, centerPoint.Y);
                if (fill)
                    g.FillEllipse(new SolidBrush(fillColor), -r, -r, 2 * r, 2 * r);
                if (drawOutLine)
                {
                    Pen pen = new Pen(outLineColor);
                    pen.Width = outLineWidth;
                    g.DrawEllipse(pen, -r, -r, 2 * r, 2 * r);
                    pen.Dispose();
                }



                g.ResetTransform();
            }
            catch (Exception ex)
            {

                WFLog.Write(ex);
            }


        }
        /// <summary>
        /// 绘制字符串
        /// </summary>
        /// <param name="g">画布</param>
        /// <param name="strToDraw">要绘制的字符串</param>
        /// <param name="fontName">字体</param>
        /// <param name="fontSize">字号</param>
        /// <param name="fontColor">颜色</param>
        /// <param name="fontStyle"></param>
        /// <param name="upLeftPoint"></param>
        public static void DrawString(Graphics g, string strToDraw, string fontName, float fontSize, Color fontColor,
        FontStyle fontStyle, PointF upLeftPoint)
        {
            try
            {
                FontFamily fontFamily = new FontFamily(fontName);
                //定义文本格式
                Font font = new Font(fontFamily, fontSize, fontStyle, GraphicsUnit.Pixel);
                g.TextRenderingHint = TextRenderingHint.AntiAlias;
                g.DrawString(strToDraw, font, new SolidBrush(fontColor),
                    upLeftPoint);
                fontFamily.Dispose();
                font.Dispose();
            }
            catch (Exception ex)
            {

                WFLog.Write(ex);
            }

        }

        /// <summary>
        /// 画箭头
        /// </summary>
        /// <param name="g"></param>
        /// <param name="angle"></param>
        /// <param name="lineWidth"></param>
        /// <param name="lineLength"></param>
        /// <param name="startPoint"></param>
        /// <param name="lineColor"></param>
        /// <param name="outLineColor"></param>
        public static void DrawLineWithArrow(Graphics g, float angle, float lineWidth, float lineLength,
            PointF startPoint, Color lineColor, Color outLineColor)
        {
            try
            {
                g.TranslateTransform(startPoint.X / 2, startPoint.Y / 2);
                g.RotateTransform(angle);
                float arrHeight = 0.14f * lineLength;

                PointF[] points = new PointF[7];
                points[0] = new PointF(-lineWidth / 2, 0);
                points[1] = new PointF(lineWidth / 2, 0);
                points[2] = new PointF(lineWidth / 2, -(lineLength - arrHeight));
                points[3] = new PointF(lineWidth / 2 + lineWidth * 0.5f, -(lineLength - arrHeight));
                points[4] = new PointF(0, -lineLength);
                points[5] = new PointF(-(lineWidth / 2 + lineWidth * 0.5f), -(lineLength - arrHeight));
                points[6] = new PointF(-lineWidth / 2, -(lineLength - arrHeight));

                GraphicsPath path = new GraphicsPath();
                path.AddPolygon(points);
                PathGradientBrush pBrush = new PathGradientBrush(path);

                pBrush.CenterColor = Color.White;
                pBrush.SurroundColors = new Color[] { lineColor };
                pBrush.FocusScales = new PointF(0, 1);
                pBrush.CenterPoint = new PointF(-(lineWidth / 2 * 0.4f), -lineLength / 2);

                g.FillPath(pBrush, path);
                g.DrawPath(new Pen(outLineColor), path);

                g.ResetTransform();

                pBrush.Dispose();
                path.Dispose();

            }
            catch (Exception ex)
            {

                WFLog.Write(ex);
            }
        }

        #endregion
        #region  字符格式转化
        /// <summary>
        /// 将byte[]转化为16进制字符串
        /// </summary>
        /// <param name="src"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static String ValueCopy(byte[] src, int length)
        {
            StringBuilder temp = new StringBuilder();
            try
            {
                //StringBuilder temp = new StringBuilder();
                char[] chars = "0123456789ABCDEF".ToCharArray();
                for (int i = 0; i < length; i++)
                {
                    byte bt = src[i];
                    int bit = (src[i] & 0x0f0) >> 4;
                    temp.Append(chars[bit]);
                    bit = src[i] & 0x0f;
                    temp.Append(chars[bit]);
                }
                return temp.ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                WFLog.Write(ex);
            }
            return temp.ToString();
        }
        /// <summary>
        /// 将byte[]数据转换成十六进制形式的string
        /// </summary>
        /// <param name="bytesPacket">byte[]数据</param>
        /// <param name="intLength">转换长度</param>
        /// <returns>十六进制形式的字符串</returns>
        public static string ToHex(byte[] bytesPacket, int intLength)
        {
            try
            {
                //检查输入的数据
                if (bytesPacket == null)
                {
                    return "";
                }
                if (intLength > bytesPacket.Length)
                {
                    return "";
                }

                //
                string strPacket = "";
                for (int i = 0; i < intLength; i++)
                {
                    //byte to string
                    strPacket += bytesPacket[i].ToString("X2") + "";
                }
                //strPacket += "";
                return strPacket;
            }
            catch (Exception e)
            {
                Console.WriteLine("WFLog: 异常 " + e.Message.ToString() + e.ToString());
                return "";
            }
        }
        public static byte[] HexToByte(string msg)
        {

            msg = msg.Replace(" ", "");//移除空格

            byte[] comBuffer = new byte[msg.Length / 2];

            for (int i = 0; i < msg.Length; i += 2)
            {

                comBuffer[i / 2] = (byte)Convert.ToByte(msg.Substring(i, 2), 16);

            }

            return comBuffer;

        }

        public static string HexStringToString(string hs, Encoding encode)
        {
            //以%分割字符串，并去掉空字符
            string[] chars = hs.Split(new char[] { '%' }, StringSplitOptions.RemoveEmptyEntries);
            byte[] b = new byte[chars.Length];
            //逐个字符变为16进制字节数据
            for (int i = 0; i < chars.Length; i++)
            {
                b[i] = Convert.ToByte(chars[i], 16);
            }
            //按照指定编码将字节数组变为字符串
            return encode.GetString(b);
        }
        //public static byte[] ToByte(string str)
        //{ 

        //}
        #endregion


        /// <summary>
        /// 长度为2Byte的长度计算
        /// </summary>
        /// <returns></returns>
        public static int getSum(byte byHight, byte byLow)
        {
            int sum = 0;
            try
            {
                int len_hight = byHight;
                int len_low = byLow;
                sum = len_hight * 256 + len_low;

            }
            catch (Exception ex)
            {

                WFLog.Write(ex);
            }
            return sum;

        }
        
        
    }
}
